package xyz.bali16.module.ban.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.module.ban.entity.Ban;
import xyz.bali16.module.ban.mapper.BanMapper;
import xyz.bali16.module.ban.model.BanAddParam;
import xyz.bali16.module.ban.model.BanPageParam;
import xyz.bali16.module.ban.model.BanUpdateParam;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class BanServiceImpl extends ServiceImpl<BanMapper, Ban> implements BanService {
    private final BanMapper banMapper;

    public BanServiceImpl(BanMapper banMapper) {
        this.banMapper = banMapper;
    }

    @Override
    public IPage<Ban> getPage(BanPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getName(),param.getUrl());
    }

    @Override
    public void addBan(BanAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "BanAddParam 参数不能为为空");
        Ban ban = new Ban();
        BeanUtils.copyProperties(param, ban);
        System.out.println(ban);
        save(ban);
    }

    @Override
    public void updateBan(BanUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "BanAddParam 参数不能为为空");
        Ban ban = new Ban();
        BeanUtils.copyProperties(param, ban);
        updateById(ban);
    }

    @Override
    public void updateStatus(String banId, Integer status) throws Exception {
        Preconditions.checkNotNull(banId, "举报id不能为空");
        if (status == null) {
            return;
        }
        Ban ban = new Ban();
        ban.setId(banId);
        ban.setStatus(status);
        updateById(ban);
    }

    @Override
    public void delete(String banId) throws Exception {
        if (banId == null) {
            return;
        }
        removeById(banId);

    }
}
